#include <myheader_c_plus.h>
#include <thread>
#include <mutex>
using std::endl;
using std::cout;
using std::mutex;
int gnum = 0;
pthread_mutex_t mux;
void * func1(void * arg)
{
    int cnt = 1000000;
    while(cnt-- > 0)
    {
        pthread_mutex_lock(&mux);
        ++gnum;
        pthread_mutex_unlock(&mux);
    }
    pthread_exit(NULL);
}
void * func2(void * arg)
{
    int cnt = 1000000;
    while(cnt-- > 0)
    {
        pthread_mutex_lock(&mux);
        ++gnum;
        pthread_mutex_unlock(&mux);
    }
    pthread_exit(NULL);

}
void * func3(void * arg)
{
    int cnt = 1000000;
    while(cnt-- > 0)
    {
        pthread_mutex_lock(&mux);
        ++gnum;
        pthread_mutex_unlock(&mux);
    }
    pthread_exit(NULL);

}
int main(int argc, char *argv[])
{
    //创建三个子线程
    pthread_t pid1, pid2, pid3;

  int ret1 =  pthread_create(&pid1,NULL,func1,NULL);
  if(ret1 == 0)
  {
      printf("thread1 quit success\n");
  }
  else
  {
      printf("thread1 quit fail\n");

  }
    int ret2 = pthread_create(&pid2,NULL,func2,NULL);
  if(ret2 == 0)
  {
      printf("thread2 quit success\n");
  }
  else
  {
      printf("thread1 quit fail\n");
  }
    int ret3 = pthread_create(&pid3,NULL,func3,NULL);
  if(ret3 == 0)
  {
      printf("thread3 quit success\n");
  }
  else
  {
      printf("thread1 quit fail\n");
  }
    pthread_join(pid1,NULL);;
    pthread_join(pid2,NULL);;
    pthread_join(pid3,NULL);;
    pthread_mutex_destroy(&mux);
    printf("gnum = %d", gnum);
    return 0;
}

